 <?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
    class Post_c extends CI_Controller {
        public function __construct() {
            parent::__construct();
            $this->load->model('post_m');
            $this->load->helper(array('form', 'url', 'html'));
            $this->load->library(array('form_validation', 'upload', 'Datatables', 'table'));
            $this->load->library('session');
        }
        
        public function index() {
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Post';
            $data['body_id'] = 'admin_post';
            $poste = "";
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            if($this->session->userdata('role') === "admin"){
                $result = $this->post_m->get_all_published_post();
                foreach ($result as $row){
                    $poste[] = array(
                    anchor('post/preview_post/'.$row->post_id, $row->title),
                    $row->lname . ", " . $row->fname . " " . $row->mname . ".",
                    date("F j, Y", strtotime($row->date_time_pub)),//F j, Y g:i a - with time
                    $row->lokasyon
                );
                }
            }else{
                $result = $this->post_m->get_all_my_post($this->session->userdata('user_id'));
                foreach ($result as $row){
                    $poste[] = array(
                    $row->title,
                    date("F j, Y", strtotime($row->date_time_pub)),
                    $row->lokasyon
                );
                }
            }
            /*echo $this->table->generate($result);*/
            
//            //if($result->count($result) > 0){
            //}
            $data['tabl'] = $poste;
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function post_list() {
            if($this->session->userdata('role') === 'author'){//author
              /*$this->datatables
                    ->select('post_id, date_time_pub, title')
                    ->from('post')
                    ->join('user', 'post.author=user.user_id')
                    ->select('fname, mname, lname, idno')
                    ->where('idno', $this->session->userdata('idno'))
                    ->where('post.status', 'published')
                    ->unset_column('post_id')
                    ->unset_column('lname')
                    ->unset_column('mname')
                    ->edit_column('fname', '$1, $2 $3', 'lname, fname, mname')
                    ->add_column('delete', anchor('post/delete/$1', 'Delete', array('onClick' => "return confirm('Are you sure you want to delete this post?')")), 'post_id');
                echo $this->datatables->generate();*/
                
                $this->datatables
                        ->query("SELECT user_id, user.fname, user.mname, user.lname, 
                user.email, user.password, post.post_id, post.title, post.summary,
                post.full_text, post.date_time_pub, post.date_time_modified, 
                post.category_id, post.author, post.status, 
                post.state,CONCAT(GROUP_CONCAT(groups.group_name SEPARATOR '/ '),'/ ', IF(bulletin_post.post_id IS NULL,'','Bulletin'))
                AS hey FROM bulletin_post
                RIGHT JOIN post USING(post_id) join user ON post.author = user.user_id 
                JOIN group_post ON post.post_id = group_post.post_id JOIN groups ON group_post.group_id = groups.group_id
                where post.status='published' GROUP BY post.post_id
                UNION
                SELECT user_id, user.fname, user.mname, user.lname, 
                user.email, user.password, post.post_id, post.title, post.summary,
                post.full_text, post.date_time_pub, post.date_time_modified, 
                post.category_id, post.author, post.status, 
                post.state,'Bulletin' AS hey FROM bulletin_post
                JOIN post USING(post_id) join user ON post.author = user.user_id 
                where post.status='published' AND post.post_id NOT IN (SELECT post_id FROM group_post)GROUP BY post.post_id");
                /*->unset_column('post_id')
                ->unset_column('lname')
                ->unset_column('mname')
                ->edit_column('fname', '$1, $2 $3', 'lname, fname, mname')
                ->add_column('delete', anchor('post/delete/$1', 'Delete', array('onClick' => "return confirm('Are you sure you want to delete this post?')")), 'post_id');*/
            echo $this->datatables->generate();
            }else{//admin
                $this->datatables
                    ->select('post_id, date_time_pub, title')
                    ->from('post')
                    ->join('user', 'post.author=user.user_id')
                    ->select('fname, mname, lname, idno')
                    ->where('post.status', 'published')
                    ->unset_column('post_id')
                    ->unset_column('mname')
                    ->unset_column('lname')
                    ->edit_column('fname', '$1, $2 $3', 'lname, fname, mname')
                    ->add_column('delete', anchor('post/delete/$1', 'Delete', array('onClick' => "return confirm('Are you sure you want to delete this post?')")), 'post_id');
                echo $this->datatables->generate();
            }
        }
        
        function pending_post_list() {
            if($this->session->userdata('role') === "admin"){
                $this->datatables
                ->select('post_id, title, date_time_pub')
                ->from('post')
                ->join('user', 'post.author=user.user_id')
                ->select('fname, mname, lname')
                ->where('post.status', 'pending')
                ->unset_column('post_id')
                ->unset_column('lname')
                ->unset_column('mname')
                ->edit_column('fname', '$1, $2 $3', 'lname, fname, mname')
                ->add_column('approve', anchor('post/pending/approve_now/$1', 'Approve', array('onClick' => "return confirm('Are you sure you want to approve this post?')")), 'post_id')
                ->add_column('reject', anchor('post/pending/reject/$1', 'Reject', array('onClick' => "return confirm('Are you sure you want to reject this post?')")), 'post_id');
           echo $this->datatables->generate();
            }else{
                $this->datatables
                ->select('post_id, title, date_time_pub')
                ->from('post')
                ->join('user', 'post.author=user.user_id')
                /*->select('fname, mname, lname')*/
                ->where('post.status', 'pending')
                ->where('post.author', $this->session->userdata('user_id'))
                ->unset_column('post_id')
                /*->unset_column('lname')
                ->unset_column('mname');*/
                ->unset_column('date_time_pub');
           echo $this->datatables->generate();
            }
           
        }
        
        function draft_post_list() {
          $this->datatables
                ->select('post_id, date_time_pub, title')
                ->from('post')
                ->join('user', 'post.author=user.user_id')
                ->where('idno', $this->session->userdata('idno'))
                ->where('post.status', 'draft')
                ->unset_column('post_id')
                ->edit_column('title',  anchor('#',"$1"),"title")
                ->add_column('publish', anchor('post/draft/publish/$1', 'Publish', array('onClick' => "return confirm('Are you sure you want to publish this post?')")), 'post_id')
                ->add_column('delete', anchor('post/draft/delete/$1', 'Delete', array('onClick' => "return confirm('Are you sure you want to delete this post?')")), 'post_id');
            echo $this->datatables->generate();
        }
        
        function rejected_post_list() {
        $this->datatables
                ->select('`comment`')
                ->select('post.post_id')
                ->select('date_time_rejected')
                ->select('title')
                ->from('notification')
                ->join('post', 'notification.post_id=post.post_id')
                ->where('post.status','rejected')
                ->where('post.author',  $this->session->userdata('user_id'))
                ->unset_column('post.post_id')
                ->add_column('delete', anchor('post/rejected/delete/$1', 'Delete', array('onClick' => "return confirm('Are you sure you want to delete this rejected post?')")), 'post.post_id');
            echo $this->datatables->generate();
        }
        
        function logout() {
            $this->session->unset_userdata('logged_in');
            session_destroy();
            redirect(base_url(), 'refresh');
        }
/*------------------------------------------------------------------------------------------------------*/
        function create_post() {
            $data['body_id'] = 'admin_create';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Create Post';
            if($this->session->userdata('role') === 'admin'){
                $data['groups'] = $this->post_m->get_groups();
            }else{
                $data['groups'] = $this->post_m->get_managed_groups($this->session->userdata('user_id'));
            }
            $data['category'] = $this->post_m->get_category();
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            /*$this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');*/
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            $this->form_validation->set_rules('category', 'Category', 'required|xss_clean');
            /*$this->form_validation->set_rules('destination', 'Destination', 'required|xss_clean');*/
            
            if ($this->form_validation->run() === FALSE) {
                
                $this->load->view('administrator/fragments/header_v', $data);
                $this->load->view('administrator/fragments/post_navigation_v', $data);
                $this->load->view('administrator/post_create_v', $data);
                $this->load->view('administrator/fragments/footer_v', $data);
                
            } else {
                if(isset($_POST['save']) && !empty($_POST['save'])) {
                    /*$temp = explode(",", $this->input->post('destination'));*/
                    $result = $this->post_m->get_max_post();

                    $data = array(
                        'post_id' => $result['post_id'] + 1,
                        'title' => $this->input->post('title'),
                        'summary' => $this->input->post('summary'),
                        'full_text' => $this->input->post('full_text'),
                        'status' => 'draft',
                        'state' => 'show',
                        'author' => $this->session->userdata('user_id'),
                        'category_id' => $this->input->post('category'),
                    );
                    $this->post_m->save_post($data);
                    if(isset($_POST['bulletin'])){
                            $this->post_m->bulletin_post($data['post_id']);
                        }
                        foreach ($this->input->post('samp') as $group_id){
                            $this->post_m->gpost($data['post_id'],$group_id);
                        }
                    redirect(base_url().'post');
                } else if(isset($_POST['publish'])) {
                    if($this->session->userdata('role') === 'admin') {

                        $result = $this->post_m->get_max_post();
                        if($this->input->post('summary') === "" || $this->input->post('summary') === null){
                           $summary = $this->input->post('full_text'); 
                        }else{
                           $summary = $this->input->post('summary');
                        }
                        
                        $data = array(
                            'post_id' => $result['post_id'] + 1,
                            'title' => $this->input->post('title'), 
                            'full_text' => $this->input->post('full_text'),
                            'summary' => $summary,
                            'status' => 'published',
                            'state' => 'show',
                            'author' => $this->session->userdata('user_id'),
                            'category_id' => $this->input->post('category'),
//                            'destination_id' => $this->input->post('destination'),
                        );
                        $this->post_m->publish_post($data);
                        if(isset($_POST['bulletin'])){
                            $this->post_m->bulletin_post($data['post_id']);
                        }
                        foreach ($this->input->post('samp') as $group_id){
                            $this->post_m->gpost($data['post_id'],$group_id);
                        }
                        
                        redirect(base_url().'post');
                    } else if($this->session->userdata('role') === 'author') {

                        $result = $this->post_m->get_max_post();
                        if($this->input->post('summary') === "" || $this->input->post('summary') === null){
                           $summary = $this->input->post('full_text'); 
                        }else{
                           $summary = $this->input->post('summary');
                        }
                        $data = array(
                            'post_id' => $result['post_id'] + 1,
                            'title' => $this->input->post('title'),
                            'summary' => $summary,
                            'full_text' => $this->input->post('full_text'),
                            'status' => 'pending',
                            'author' => $this->session->userdata('user_id'),
                            'category_id' => $this->input->post('category'),
                        );

                        $this->post_m->publish_post($data);
                        if(isset($_POST['bulletin'])){
                            $this->post_m->bulletin_post($data['post_id']);
                        }
                        foreach ($this->input->post('samp') as $group_id){
                            $this->post_m->gpost($data['post_id'],$group_id);
                        }
                        redirect(base_url().'post');
                    }
                }
            }
        }
        
        
        function edit_post($post_id) {
            $data['body_id'] = 'admin_post';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $post = $this->post_m->get_published_post($post_id);
            $data['title'] = 'Edit Post';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            $this->form_validation->set_rules('state', 'State', 'required|xss_clean');
            
            if(isset($_POST) && !empty($_POST)) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'summary' => $this->input->post('summary'),
                    'full_text' => $this->input->post('full_text'),
                    'state' => $this->input->post('state')
                );
                
                if ($this->form_validation->run() === TRUE) {
                    
                    $this->post_m->update_post($post_id, $data);
                    
                    $this->session->set_flashdata('success', 'Post successfully updated.');
                    
                    redirect(base_url().'post');
                } 
                
            }
            
            $data['post'] = $post;
            
            $data['post_title'] = array(
                'name' => 'title',
                'id' => 'edit_post_title',
                'type' => 'text',
                'value' => $this->form_validation->set_value('title', $post['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                'id' => 'edit_post_summary',
                'type' => 'text',
                'value' => $this->form_validation->set_value('summary', $post['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'edit_post_full_text',
                'class' => 'summary_editor',
                'type' => 'text',
                'value' => $this->form_validation->set_value('full_text', $post['full_text'])
            );
            
            $data['show'] = array(
                'name' => 'state',
                'id' => 'edit_post_show',
                'type' => 'radio',
                'checked' => ($this->form_validation->set_value('type', $post['state']) == 'show')? TRUE : FALSE,
                'value' => 'show'
            );
            
            $data['hide'] = array(
                'name' => 'state',
                'id' => 'edit_post_hide',
                'type' => 'radio',
                'checked' => ($this->form_validation->set_value('type', $post['state']) == 'hide')? TRUE : FALSE,
                'value' => 'hide'
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_edit_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);  
        }
        
        function delete_post($post_id){
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post();
            $post = $this->post_m->get_trashed_post($post_id);
            
            $data = array(
                'title' => $post['title'],
                'summary' => $post['summary'],
                'full_text' => $post['full_text'],
                'date_time_pub' => $post['date_time_pub'],
                'date_time_modified' => $post['date_time_modified'],
                'date_time_deleted' => date('Y-m-d h:i:s'),
                'category_id' => $post['category_id'],
                'author' => $post['user_id'],
                'destination_id' => $post['destination_id'],
                'group_id' => $post['group_id'],
                'status' => $post['status'],
                'state' => 'hide'
            );
            $this->post_m->delete_post($post_id);
            
            $this->session->set_flashdata('success', 'Post successfully deleted.');
            
            redirect(base_url().'post');
        }
/*------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------PENDING POST---------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/
        function show_pending_post() {
            $data['body_id'] = 'admin_pending';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Pending Post';
            $pending = "";
            if($this->session->userdata('role') === "admin"){
                $result = $this->post_m->get_all_pending_post();
                foreach ($result as $row){
                    $pending[] = array(
                    $row->title,
                    $row->lname . ", " . $row->fname . " " . $row->mname . ".",
                    "<a href='".base_url()."post/pending/approve_now/".$row->post_id."' onClick='return confirm('Are you sure you want to approve this post?')'>Approve</a>",
                    "<a href='".base_url()."post/pending/reject/".$row->post_id."' onClick='return confirm('Are you sure you want to reject this post?'>Reject</a>"
                   /*
                    * ->add_column('approve', anchor('post/pending/approve_now/$1', 'Approve', array('onClick' => "return confirm('Are you sure you want to approve this post?')")), 'post_id')
                ->add_column('reject', anchor('post/pending/reject/$1', 'Reject', array('onClick' => "return confirm('Are you sure you want to reject this post?')")), 'post_id');
                    */
                );
                }
            }else{
                $result = $this->post_m->get_my_pending_post($this->session->userdata('user_id'));
                                foreach ($result as $row){
                    $pending[] = array(
                    $row->title
                    
                   /*
                    * ->add_column('approve', anchor('post/pending/approve_now/$1', 'Approve', array('onClick' => "return confirm('Are you sure you want to approve this post?')")), 'post_id')
                ->add_column('reject', anchor('post/pending/reject/$1', 'Reject', array('onClick' => "return confirm('Are you sure you want to reject this post?')")), 'post_id');
                    */
                );
                }
            }
            $data['tabl'] = $pending;
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_pending_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function approve_pending_post_now($post_id) {
            $pending = $this->post_m->get_pending_post($post_id);
            
            $data = array(
                    'title' => $pending['title'],
                    'summary' => $pending['summary'],
                    'full_text' => $pending['full_text'],
                    'full_text' => $pending['full_text'],
                    'status' => 'published',
                    'state' => 'show'
                );
            
            $this->post_m->publish_pending_post($post_id, $data);
            
            $this->session->set_flashdata('success', 'Content Published');
            
            redirect(base_url().'post/pending/view');
            
        }
        
        function approve_pending_post($post_id) {
            $data['body_id'] = 'admin_pending';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $pending = $this->post_m->get_pending_post($post_id);
            $data['title'] = 'Approve Draft';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            
            if(isset($_POST) && !empty($_POST)) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'summary' => $this->input->post('summary'),
                    'full_text' => $this->input->post('full_text'),
                    'status' => 'published',
                    'state' => 'show'
                );
                
                if ($this->form_validation->run() === TRUE) {
                    
                    $this->post_m->publish_pending_post($post_id, $data);
                    
                    $this->session->set_flashdata('success', 'Post approved.');
                    
                    redirect(base_url().'post/pending/view');
                } 
                
            }
            
            $data['pending'] = $pending;
            
            $data['pending_title'] = array(
                'name' => 'title',
                'id' => 'approve_pending_title',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('title', $pending['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                'id' => 'approve_pending_summary',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('summary', $pending['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'approve_pending_full_text',
                'class' => 'summary_editor',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('full_text', $pending['full_text'])
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/post_pending_approve_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function reject_pending_post($post_id){
            $data['body_id'] = 'admin_pending';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $reject = $this->post_m->get_rejected_post($post_id);
            $data['title'] = 'Reject Draft';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            $this->form_validation->set_rules('comment', 'Comment', 'required|xss_clean');
            
            if(isset($_POST)&& !empty($_POST)){
                $data = $this->input->post('comment');
                
                if($this->form_validation->run() === TRUE) {
                    $this->post_m->insert_update_notification($post_id, $data);
                    
                    redirect(base_url().'post/pending/view');
                }
            }
            $data['rejected'] = $reject;
            
            $data['reject_title'] = array(
                'name' => 'title',
                'id' => 'reject_post_title',
                'readonly' => "readonly",
                'value' => $reject['title']
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'reject_post_full_text',
                'readonly' => "readonly",
                'value' => $reject['full_text']
            );
            
            $data['comment'] = array(
                'name' => 'comment',
                'id' => 'reject_post_comment',
                'placeholder' => 'Put comment here.'
            );
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_pending_reject_v',$data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
/*------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------DELETED POST---------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/        
        function post_trash() {
            $data['body_id'] = 'admin_post';
            $data['title'] = 'Deleted Post';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['trash'] = $this->post_m->get_all_trashed_post();
            $data['message'] = $this->session->flashdata('message');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/post_trash_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function recover_trashed_post($post_id) {
            $data = array(
                'date_time_modified' => date('Y-m-d h:i:s'),
                'status' => 'published'
            );
            $this->post_m->recover_trashed_post($post_id, $data);
            
            $this->session->set_flashdata('message', 'Post successfully recovered.');
            
            redirect(base_url().'post/trash');
        }
        
        function delete_trashed_post($trash_id) {
            $this->post_m->delete_trashed_post($trash_id);
            
            $this->session->set_flashdata('message', 'Post permanently deleted.');
            
            redirect(base_url().'post/trash');
        }
/*------------------------------------------------------------------------------------------------------*/
/*----------------------------------------------DRAFT POST----------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/
        function show_draft_post($author) {
            $data['body_id'] = 'admin_draft';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Pending Post';
            $data['draft'] = $this->post_m->get_all_drafted_post($author);
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_draft_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function sort_draft_post($user, $order) {
            $data['body_id'] = 'admin_draft';
//            $order = '';
//            if($o === TRUE) {
//                $order = '';
//            } else {
//                $order = 'DESC';
//            }
//            
//            $data['o'] = $o;
//            $data['u'] = $user;
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Pending Post';
            $data['draft'] = $this->post_m->sort_draft($order, $user);
            $data['error'] = $this->session->flashdata('error');
            $data['success'] = $this->session->flashdata('success');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_draft_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function edit_draft_post($post_id) {
            $data['body_id'] = 'admin_draft';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $draft = $this->post_m->get_drafted_post($post_id);
            $data['title'] = 'Edit Draft';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            if ($this->form_validation->run() === TRUE) {
                if(isset($_POST['save']) && !empty($_POST['save'])) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'summary' => $this->input->post('summary'),
                        'full_text' => $this->input->post('full_text'),
                        'status' => 'draft',
                        'state' => 'show'
                    );
                    $this->post_m->save_drafted_post($post_id, $data);
                    
                    $this->session->set_flashdata('message', 'Draft saved.');
                    
                    redirect('post/draft/'.$this->session->userdata('user_id'));
                }else if(isset ($_POST['publish']) && !empty ($_POST['publish'])) {
                    $data = array(
                        'title' => $this->input->post('title'),
                        'summary' => $this->input->post('summary'),
                        'full_text' => $this->input->post('full_text'),
                        'status' => 'published',
                        'state' => 'show'
                    );
                    $this->post_m->publish_drafted_post($post_id, $data);
                    $this->session->set_flashdata('message', 'Draft published.');
                    redirect('post/draft/'.$this->session->userdata('user_id'));
                }
            }   
            
            
            $data['draft'] = $draft;
            
            $data['draft_title'] = array(
                'name' => 'title',
                'id' => 'post_title',
                'type' => 'text',
                'value' => $this->form_validation->set_value('title', $draft['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                /*'id' => 'approve_draft_summary',*/
                'type' => 'text',
                'value' => $this->form_validation->set_value('summary', $draft['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'post_full_text',
                'type' => 'text',
                'value' => $this->form_validation->set_value('full_text', $draft['full_text'])
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_draft_edit_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function publish_draft_post($post_id) {
            $data['body_id'] = 'admin_draft';
            $draft = $this->post_m->get_drafted_post($post_id);
            if($this->session->userdata('role') === "admin"){
                $status = "published";
            }else{
                $status = "pending";
            }
            $data = array(
                        'title' => $draft['title'],
                        'summary' => $draft['summary'],
                        'full_text' => $draft['full_text'],
                        'status' => $status,
                        'state' => 'show'
                    );
                    $this->post_m->publish_drafted_post($post_id, $data);
                    $this->session->set_flashdata('message', 'Draft published.');
                    redirect('post/draft/'.$this->session->userdata('user_id'));
        }
        
        function delete_draft_post($post_id) {
            $this->post_m->delete_drafted_group_post($post_id);
            $this->post_m->delete_drafted_bulletin_post($post_id);
            $this->post_m->delete_drafted_post($post_id);
            if($this->uri->segment(2) === "draft"){
                $this->session->set_flashdata('message', 'Draft permanently deleted.');
                redirect(base_url().'post/draft/'.$this->session->userdata('user_id'));    
            }else{
                $this->session->set_flashdata('message', 'rejected permanently deleted.');
                redirect(base_url().'post/rejected');    
            }
        } 
/*------------------------------------------------------------------------------------------------------*/
/*--------------------------------------------REJECTED POST---------------------------------------------*/
/*------------------------------------------------------------------------------------------------------*/
        function show_rejected_post() { 
            $data['body_id'] = 'admin_reject';
            $data['title'] = 'Pending Post';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
//            $data['rejected'] = $this->post_m->get_all_rejected_post($this->session->userdata('user_id'));
            $data['message'] = $this->session->flashdata('message');
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/post_reject_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function reject_edit_post($post_id) {
            $data['body_id'] = 'admin_reject';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $rejected = $this->post_m->get_rejected_post($post_id);
            $data['title'] = 'Edit Post';
            
            $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
            $this->form_validation->set_rules('summary', 'Summary', 'required|xss_clean');
            $this->form_validation->set_rules('full_text', 'Full Text', 'required|xss_clean');
            
            if(isset($_POST) && !empty($_POST)) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'summary' => $this->input->post('summary'),
                    'full_text' => $this->input->post('full_text'),
                    'status' => 'pending',
                    'state' => 'show'
                );
                
                if ($this->form_validation->run() === TRUE) {
                    
                    $this->post_m->publish_pending_post($post_id, $data);
                    
                    $this->session->set_flashdata('message', 'Post re-published successfully.');
                    
                    redirect(base_url().'post/rejected');
                } 
                
            }
            
            $data['rejected'] = $rejected;
            
            $data['rejected_title'] = array(
                'name' => 'title',
                'id' => 'rejected_title',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('title', $rejected['title'])
            );
            
            $data['summary'] = array(
                'name' => 'summary',
                'id' => 'rejected_summary',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('summary', $rejected['summary'])
            );
            
            $data['full_text'] = array(
                'name' => 'full_text',
                'id' => 'rejected_full_text',
                'class' => 'summary_editor',
                'type' => 'text',
                'readonly' => TRUE,
                'value' => $this->form_validation->set_value('full_text', $rejected['full_text'])
            );
            
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/post_reject_edit_v', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
        
        function preview_post($post_id) {
            $data['body_id'] = 'admin_preview_post';
            if($this->session->userdata('role') === "admin"){
                $data['num_pending'] = $this->post_m->count_pending_post();
            }else{
                $data['num_pending'] = $this->post_m->count_my_pending_post($this->session->userdata('user_id'));
            }
            $data['num_reject'] = $this->post_m->count_rejected_post($this->session->userdata('user_id'));
            $data['num_draft'] = $this->post_m->count_drafted_post($this->session->userdata('user_id'));
            $data['num_trash'] = $this->post_m->count_trashed_post();
            $data['title'] = 'Preview';
            $data['full_story'] = $this->post_m->get_bulletin_full_text_post($post_id);
            $data['day'] = array(
                'name' => 'day',
                'id' => 'day_filter',
                'type' => 'text'
            );
            $data['submit_day'] = array(
                'name' => 'submit_day',
                'id' => 'submit_day_filter',
                'type' => 'submit',
                'value' => 'FIND'
            );
            $data['year'] = array(
                'name' => 'year',
                'id' => 'year_filter',
                'type' => 'text'
            );
            $data['submit_year'] = array(
                'name' => 'submit_year',
                'id' => 'submit_year_filter',
                'type' => 'submit',
                'value' => 'FIND'
            );
            $data['img'] = array(
                'src' => 'resources/images/backButton.png',
                'alt' => ':)',
                'id' => 'back_image'
            );
            $this->load->view('administrator/fragments/header_v', $data);
            $this->load->view('administrator/fragments/post_navigation_v', $data);
            $this->load->view('administrator/preview_post', $data);
            $this->load->view('administrator/fragments/footer_v', $data);
        }
    }
?>
